package com.wl.basic.service.impl;

import com.wl.basic.mapper.BaseMapper;
import com.wl.basic.query.BaseQuery;
import com.wl.basic.service.BaseService;
import com.wl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    //注入mapper
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    //默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();

    }

   /* @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        return null;
    }*/

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //查询总条数
        Integer totals = baseMapper.queryCount(baseQuery);
        if (totals == 0) {
            return new PageList<>();
        }
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(totals, data);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void removeA(List<Long> ids) {
        baseMapper.removeA(ids);
    }


}
